package com.gardenguell.salad.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import com.gardenguell.salad.R;
import com.gardenguell.salad.data.model.Box;
import com.gardenguell.salad.ui.activity.base.ToolbarActivity;
import com.gardenguell.salad.ui.fragment.HistoryFragment;
import com.gardenguell.salad.ui.fragment.UnitFragment;
import com.gardenguell.salad.ui.fragment.VegetableFragment;

import javax.crypto.Mac;

/**
 * Created by absurd on 16/2/10.
 */
public class BoxActivity extends ToolbarActivity {

    private FragmentTabHost mTabHost;

    public Box box;


    @Override
    protected int provideContentViewId() {
        return R.layout.activity_box;
    }


    @Override
    public boolean canBack() {
        return false;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        box = (Box)getIntent().getExtras().get("box");
        initTabView();
        mTabHost.setCurrentTabByTag("home");
    }


    private void initTabView(){
        LayoutInflater inflater = LayoutInflater.from(this);
        Class fragmentArray[] = {HistoryFragment.class, UnitFragment.class, VegetableFragment.class};
        String textArray[] = {"history", "home", "knowledge"};
        int viewArray[] = {R.drawable.sel_tab_history, R.drawable.sel_tab_home, R.drawable.sel_tab_knowledge};

        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setBackgroundResource(R.drawable.sel_tab_background);
        // Fragment嵌套Fragment,应该使用fragment的getChildFragmentManager
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        for(int i = 0; i < fragmentArray.length; i++){
            View indicateView = inflater.inflate(R.layout.tab_item_view, null);
            ImageView image = (ImageView)indicateView.findViewById(R.id.img_tab);
            image.setImageResource(viewArray[i]);

            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(textArray[i]).setIndicator(indicateView);
            mTabHost.addTab(tabSpec, fragmentArray[i], null);
            mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.sel_tab_background);
        }
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                mTabHost.setCurrentTabByTag(tabId);
            }
        });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(mTabHost.getCurrentTabTag().equals("history")){
            HistoryFragment fragment = (HistoryFragment)getSupportFragmentManager().findFragmentByTag("history");
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }


}
